package org.votes.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.votes.bean.Details;
import org.votes.service.DetailsService;
import org.votes.service.ManagerService;
import org.votes.util.DetailsOperationUtil;


@Controller
public class DetailsController {

	@Autowired
	private DetailsService detailsService;
	
	@Autowired
	private ManagerService managerService;
	
	@RequestMapping("/details/result")
	public ModelAndView homePage() {
		Map<String, Object> model = new HashMap<String, Object>();
		List<Details> list = detailsService.findAll();
		model.put("list", list);
		return new ModelAndView("/details/result", model);
	}
	
	@RequestMapping(value = "findAll")
	@ResponseBody  
	public ModelAndView findAll() {
		Map<String, Object> model = new HashMap<String, Object>();
		List<Details> list = detailsService.findAll();
		model.put("list", list);
		return new ModelAndView("/votes/votes", model);
	}
	
	@RequestMapping(value = "/details/addDetails")
	@ResponseBody  
	public String add(@RequestParam(value="title", required=true) String title,
			@RequestParam(value="answer1", required=true) String answer1,
			@RequestParam(value="answer2", required=true) String answer2,
			@RequestParam(value="answer3", required=true) String answer3,
			@RequestParam(value="answer4", required=true) String answer4,
			Model model) {
		
		Details detail = new Details(title, answer1, answer2, answer3, answer4, 0, 0, 0, 0);
		try {
			detailsService.add(detail);
		} catch (Exception e) {
			return "ERROR";
		}
		return "OK";
	}
	
	@RequestMapping(value = "/details/lookVotes")
	@ResponseBody
	public ModelAndView lookVotes() {
		HashMap<String, List<Details>> map = new HashMap<String, List<Details>>();
		HashMap<Integer, Object[]> detailMap = DetailsOperationUtil.detailMap;
		for(Map.Entry<Integer, Object[]> entry : detailMap.entrySet()) {
			Integer id = entry.getKey();
			Details detail = detailsService.findById(id);
			Object[] objs = entry.getValue();
			int i = 1;
			for(Object obj : objs) {
				if(obj != null) {
					ConcurrentHashMap<Integer, Integer> data = (ConcurrentHashMap<Integer, Integer>) obj;
					if(i == 1) {
						detail.setCount1(detail.getCount1() + data.get(1));
						data.put(1, 0);
					}else if (i == 2) {
						detail.setCount2(detail.getCount2() + data.get(2));
						data.put(2, 0);
					}else if (i == 3) {
						detail.setCount3(detail.getCount3() + data.get(3));
						data.put(3, 0);
					}else if (i == 4) {
						detail.setCount4(detail.getCount4() + data.get(4));
						data.put(4, 0);
					}
					i++;
				}
			}
			detailsService.update(detail);
		}
		List<Details> list = detailsService.findAll();
		map.put("list", list);
		return new ModelAndView("/details/votesResult", map);
	}
	
	
	@RequestMapping(value = "/details/updateDetails")
	@ResponseBody 
	public String update(@RequestParam(value="string", required=true) String string,
			Model model) {
		try {
			
			String[] strs = string.split(";");
			for(String str : strs) {
				String[] detailStr = str.split(",");
				Integer id = Integer.parseInt(detailStr[0]);
				Integer count = Integer.parseInt(detailStr[1]);
				DetailsOperationUtil.updateCount(id, count);
			}
		} catch (Exception e) {
			System.out.println(e);
			return "ERROR";
		}
		return "OK";
	}
	
	private Details setCount(int count, Details detail) {
		if(count == 1) {
			int count1 = detail.getCount1();
			detail.setCount1(count1 + 1);
		}else if (count == 2) {
			int count2 = detail.getCount2();
			detail.setCount2(count2 + 1);
		}else if (count == 3) {
			int count3 = detail.getCount3();
			detail.setCount3(count3 + 1);
		}else if (count == 4) {
			int count4 = detail.getCount4();
			detail.setCount4(count4 + 1);
		}
		return detail;
	}
	
}
